<?php
/**
 * Created by PhpStorm.
 * User: minicart
 * Date: 2018/11/4
 * Time: 0:57
 */
namespace app\models;

use app\admin\service\CommonService;
use think\Model;

/**
 * Class CourseLogModel
 * @package app\models
 */
class CourseLogModel extends Model
{
    protected $table = 'edu_course_log';

    /**
     * 订单列表 分页
     * @param array $where
     * @param int $page
     * @param int $limit
     * @param string $order
     * @return array
     * @throws \think\exception\DbException
     */
    public static function pageLists($where = [], $page = 1, $limit = 5, $order = 'id desc',$where_date)
    {
        $ContractModel = new ContractModel();
        if(count($where_date) > 0 || (isset($where['grades_id']) && '' !== $where['grades_id'])){
            if(count($where_date) > 0){
                $where_contract['signing_date'] = array('between', array($where_date['start_date'],$where_date['end_date']));
            }
            if (isset($where['contract_number']) && '' !== $where['contract_number']) {
                $where_contract['contract_number'] = ['like','%'.$where['contract_number'].'%'];
            }
            if (isset($where['grades_id']) && '' !== $where['grades_id']) {
                $res_grades_id = GradesContractModel::where(['grades_id' => $where['grades_id']])->column('contract_id');
                $where_contract['id'] = ['in', implode(',',$res_grades_id)];
            }
            $result_contract = $ContractModel->where($where_contract)->column('contract_number');
            $where['contract_number'] = array('in',implode(',',$result_contract));
            unset($where['grades_id']);
        }else{
            if (isset($where['contract_number']) && '' !== $where['contract_number']) {
                $where['contract_number'] = ['like','%'.$where['contract_number'].'%'];
            }
        }

        $result =self::where($where)->order($order)->paginate($limit,false,['page'=>$page]);

        if(!$result->total()){
            return ['code' => 0, 'msg' => "ok", 'count' =>0, 'data' => []];
        }


        $listData = [];
        foreach ($result as $key => $value) {
            $course_arr = explode(',',$value['course']);
            $contract = ContractModel::where('contract_number',$value['contract_number'])->find();
            foreach ($course_arr as $v) {
                $item = [];
                $item['course'] = CourseModel::where('id', $v)->value('course_name');
                $item['campus'] = CampusModel::where('id',$value['campus'])->value('campus_name');
                if($value['record_status'] == 1){  //课程
                    $res = CourseContractModel::get(['contract_id' => $contract['id'], 'course_id' => $v]);
                    $total_amount = $res['net_receipts'] - $res['assessed_amount'];
                    $item['total_amount'] = Number_format($total_amount / 100,2);
                    $item['contract_type'] =  DictionaryModel::where('id',$res['contract_type'])->value('name');
                }else if($value['record_status'] == 2){  //班级
                    $res = GradesContractModel::get(['contract_id' => $contract['id'], 'course_id' => $v]);
                    $total_amount = $res['net_receipts'] - $res['assessed_amount'];
                    $item['total_amount'] = Number_format($total_amount / 100,2);
                    $item['grades_name'] = GradesDetailsModel::get(['id' => $res['grades_id']])['grades_name'];
                    $item['contract_type'] =  DictionaryModel::where('id',$res['contract_type'])->value('name');
                }
                $item['student_name'] = $contract['student_name'];
                $item['contract_number'] = $contract['contract_number'];
                if($contract['input_type'] == 0){
                    $item['input_type_text'] = '<span style="color:#FF5722;font-weight: bold;">新生</span>';
                }else if($contract['input_type'] == 1){
                    $item['input_type_text'] = '<span style="color:#01AAED;font-weight: bold;">老生</span>';
                }
                $item['signing_date'] = $contract['signing_date'];
                if($contract['status'] == 1){
                    $item['status_course_text'] = '<span style="color:#01AAED;font-weight: bold;">已付款</span>';
                }else if($contract['status'] == 2){
                    $item['status_course_text'] = '<span style="color:#FF5722;font-weight: bold;">未付款</span>';
                }else if($contract['status'] == 3) {
                    $item['status_course_text'] = '<span style="color:#FFB800;font-weight: bold;">部分付款</span>';
                }
                $listData[] = $item;
            }

        }
        return ['code' => 0, 'msg' => "ok", 'count' => count($listData), 'data' => $listData];
    }

    /**
     * 状态转换
     * @param $value
     * @param $data
     * @return mixed
     */
    public function getStatusTextAttr($value, $data)
    {
        $arr = config('course.status');
        return $arr[$data['status']];
    }
}